package com.jt.common.response;

import java.io.Serializable;

public class R<T> implements Serializable {

	private static int code;// 此处本准备用枚举代替，但是jackson对枚举的序列化支持并不是特别好

	private  String message;

	private T result;



	public R() {
	    this.code = ResponseCode.SUCCESS.getCode();
        this.message = ResponseCode.SUCCESS.getMessage();
	}

	public R(ResponseCode reponseCode) {
		this.code = reponseCode.getCode();
		this.message = reponseCode.getMessage();
	}

	public R(ResponseCode reponseCode, String message) {
		this.code = reponseCode.getCode();
		this.message = message;
	}

	public R(int code, String message) {
		this.code = code;
		this.message = message;
	}

	public R<T> success(T result) {
		this.code = ResponseCode.SUCCESS.getCode();
		this.message = ResponseCode.SUCCESS.getMessage();
		this.result = result;
		return this;
	}
	public  static  R ok() {
		R r = new R();
		r.setCode(ResponseCode.SUCCESS.getCode());
		r.setMessage(ResponseCode.SUCCESS.getMessage());
		return r;
	}

	public  static<T>  R<T> ok(T data) {
		R r = new R();
		r.setCode(ResponseCode.SUCCESS.getCode());
		r.setMessage(ResponseCode.SUCCESS.getMessage());
		r.setResult(data);
		return r;
	}

	public static R error() {
		R r = new R();
		r.setCode(ResponseCode.UNKOWN_EXCEPTION.getCode());
		r.setMessage(ResponseCode.UNKOWN_EXCEPTION.getMessage());

		return r;
	}
	public static R error(String message) {
		R r = new R();
		r.setCode(ResponseCode.UNKOWN_EXCEPTION.getCode());
		r.setMessage(message);

		return r;
	}
	public static R errorParam(String message) {
		R r = new R();
		r.setCode(ResponseCode.ILLEGAL_PARAM.getCode());
		r.setMessage(message);
		return r;
	}

	public R<T> failed(int code, String message, T result) {
		this.code = code;
		this.message = message;
		this.result = result;
		return this;
	}

	public int getCode() {
		return code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public T getResult() {
		return result;
	}

	public void setResult(T result) {
		this.result = result;
	}

	@Override
	public String toString() {
		return "Response [code=" + code + ", message=" + message + ", result=" + result + "]";
	}

}
